#include "pch.h"
#include "ErrorCodes.h"

using namespace DXBase;

void ExHelper::Throw(ErrorCodes c)
{
	throw CreateException(c);
}
Platform::Exception^ ExHelper::CreateException(ErrorCodes c)
{
	auto hr = MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, (int)c);
	return ref new Platform::COMException(hr);
}
Windows::Foundation::HResult ExHelper::CreateWinRTException(ErrorCodes c)
{
	auto hr = MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, (int)c);
	Windows::Foundation::HResult v = { hr };
	return v;
}
ErrorCodes ExHelper::GetCode(Platform::Exception^ ex)
{
	if (!ex)
		throw ref new Platform::NullReferenceException();

	auto hr = ex->HResult;
	Windows::Foundation::HResult v = { hr };
	return GetCode(v);
}
ErrorCodes ExHelper::GetCode(Windows::Foundation::HResult hr)
{
	auto sev = HRESULT_SEVERITY(hr.Value);
	auto fac = HRESULT_FACILITY(hr.Value);
	auto code = HRESULT_CODE(hr.Value);

	if (sev != SEVERITY_ERROR || fac != FACILITY_ITF)
		return ErrorCodes::NotErrorCode;
	if(code <= (int)ErrorCodes::CodeStart || code >= (int)ErrorCodes::CodeEnd-1)
		return ErrorCodes::NotErrorCode;

	return (ErrorCodes)code;
}
